/*
 * hal_adc.c
 *
 *  Created on: Aug 25, 2024
 *      Author: zhu
 */
#include "hal_adc.h"
#include "ch32v20x_adc.h"
#include "ch32v20x_dma.h"

static void DMA_Tx_Init(DMA_Channel_TypeDef *DMA_CHx, u32 ppadr, u32 memadr, u16 bufsize)
{
    DMA_InitTypeDef DMA_InitStructure = {0};

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    DMA_DeInit(DMA_CHx);
    DMA_InitStructure.DMA_PeripheralBaseAddr = ppadr;
    DMA_InitStructure.DMA_MemoryBaseAddr = memadr;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = bufsize;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA_CHx, &DMA_InitStructure);
}

int8_t ADC1_Function_Init(void)
{
    if(adc1.adc_pin == 0 || adc1.adc_cnt == 0 || adc1.adc_value_size == 0 || adc1.adc_value == 0) return -1;

    ADC_InitTypeDef  ADC_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_ADCCLKConfig(RCC_PCLK2_Div8);
    for(uint8_t i = 0;i<adc1.adc_cnt;i++)
    {
        adc1.adc_pin[i].init(GPIO_Mode_AIN);
    }
    ADC_DeInit(ADC1);
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel = adc1.adc_cnt;
    ADC_Init(ADC1, &ADC_InitStructure);


    ADC_DMACmd(ADC1, ENABLE);
    ADC_Cmd(ADC1, ENABLE);

    ADC_ResetCalibration(ADC1);
    while(ADC_GetResetCalibrationStatus(ADC1));
    ADC_StartCalibration(ADC1);
    while(ADC_GetCalibrationStatus(ADC1));
    return 1;
}

void ADC1_DMA_CONF(void)
{
    if(ADC1_Function_Init() == -1) return;
    DMA_Tx_Init(DMA1_Channel1, (u32)&ADC1->RDATAR, (u32)adc1.adc_value, adc1.adc_value_size);
    DMA_Cmd(DMA1_Channel1, ENABLE);
//    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_239Cycles5);
    for(uint8_t i = 0,y = 0;i < adc1.adc_cnt;i++)
    {
            if(adc1.adc_pin[i].read == pa0.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_0, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pa1.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_1, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pa2.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_2, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pa3.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_3, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pa4.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_4, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pa5.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_5, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pa6.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_6, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pa7.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_7, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pb0.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_8, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pb1.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_9, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pc0.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_10, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pc1.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_11, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pc2.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_12, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pc3.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_13, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pc4.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_14, ++y, ADC_SampleTime_239Cycles5);
            }
            else if(adc1.adc_pin[i].read == pc5.read)
            {
                ADC_RegularChannelConfig(ADC1, ADC_Channel_15, ++y, ADC_SampleTime_239Cycles5);
            }
    }
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    delay_ms(50);
}

struct adc_type adc1 ={
  0,
  0,
  0,
  0,
  ADC1_DMA_CONF
};

///////////////////////ADC   TEST//////////////////////////////////
//uint16_t adc_buf[2];
//void main(void)
//{
//struct gpio_type adc_pin[1] ={pa1,pa2};   //pa1���뵽adc��
//adc1.adc_cnt = 2;//ADC�����ĸ���
//adc1.adc_pin = adc_pin;//ʹ�õ���ADC����
//adc1.adc_value = adc_buf;//���û���
//adc1.adc_value_size = 2;//ʹ�õ��Ļ����С
//adc1.init();
//    while(1)
//    {
//        printf("pa1=%d,pa2=%d\r\n",adc_buf[0],adc_buf[1])
//    }
//}

